package com.csgc.camera.biz.other.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder

import com.csgc.camera.bean.ChooseMaskStyle

import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ItemChooseMaskStyleBinding

class ChooseMaskStyleAdapter(val context:Context,val list:ArrayList<ChooseMaskStyle>):Adapter<ChooseMaskStyleAdapter.ChooseMaskStyleHolder>() {

    private var checkedPosition = -1

    //设置选中的
    fun setCheckedPosition(position:Int){
        checkedPosition = position
        notifyDataSetChanged()
    }

    fun updateContent(data:ArrayList<ChooseMaskStyle>){
        list.addAll(data)
        notifyDataSetChanged()
    }
    private val normalTextColor:Int by lazy {
        ContextCompat.getColor(context, R.color.color_666666)
    }
    private val checkedTextColor:Int by lazy {
        ContextCompat.getColor(context, R.color.color_00CDC0)
    }

    var listener:OnItemChooseListener?=null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChooseMaskStyleHolder {
        return ChooseMaskStyleHolder(ItemChooseMaskStyleBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: ChooseMaskStyleHolder, @SuppressLint("RecyclerView") position: Int) {

        val data = list[position]
        holder.binding.apply {
            tvStyleName.text = data.name
            if (checkedPosition == position){
                root.isSelected = true
                tvStyleName.setTextColor(checkedTextColor)
            }else{
                root.isSelected = false
                tvStyleName.setTextColor(normalTextColor)
            }

            root.clickWithTrigger {
                checkedPosition = position
                notifyDataSetChanged()
                listener?.onItemChoose(data.maskType,data.maskId)
            }

        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class ChooseMaskStyleHolder(val binding:ItemChooseMaskStyleBinding):ViewHolder(binding.root)

    interface OnItemChooseListener{
        fun onItemChoose(type:Int,id:Int)
    }
}